/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * formBaoCaoCongNo.java
 *
 * Created on Jun 22, 2013, 3:32:39 PM
 */

package GUI;

import BUS.BaoCaoCongNoBUS;
import BUS.KhachHangBUS;
import Entity.BaoCaoCongNo;
import Entity.CTBaoCaoCongNo;
import Entity.KhachHang;
import Util.ComboBoxTagModel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author copang
 */
public class formBaoCaoCongNo extends javax.swing.JPanel {

    private BaoCaoCongNoBUS baoCaoBUS;
    private KhachHangBUS khachHangBUS;
    
    private ArrayList listBaoCao = null;
    private ArrayList listCTBaoCao = null;
    
    /** Creates new form formBaoCaoCongNo */
    public formBaoCaoCongNo() {
        initComponents();
        baoCaoBUS = new BUS.BaoCaoCongNoImpl();
        khachHangBUS = new BUS.KhachHangImpl();
        
        tbChiTiet.getSelectionModel().addListSelectionListener( new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent lse) {
                int index = tbChiTiet.getSelectedRow();
                if (index < 0) return; 
                
                if (index >= listCTBaoCao.size()) return;
                
                int maKH = (Integer) tbChiTiet.getValueAt(index, 1);
                loadThongTinKhachHang(maKH);
            }
            
        });
        
        loadMaBaoCao();
    }
    
    protected void loadThongTinKhachHang(int maKH) {
        KhachHang data = khachHangBUS.getByID(maKH);
        if (data == null) return;
        
        txtHoTen.setText(data.getHoTenKH());
        txtDiaChi.setText(data.getDiaChiKH());
        txtSDT.setText(data.getDienThoaiKH());
        txtEmail.setText(data.getEmailKH());
        if (data.getGioiTinh()) {
            txtGioiTinh.setText("Nam");
        } else {
            txtGioiTinh.setText("Nữ");
        }
    }
    
    protected void createReport() {        
        Date date = new Date();
        int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
        int month = Integer.parseInt(new SimpleDateFormat("MM").format(date));        
        
        if ( baoCaoBUS.add(month, year) ) {
            JOptionPane.showMessageDialog(this, "Đã lập báo cáo");
        } else {
            JOptionPane.showMessageDialog(this, "Lập báo cáo không thành công");
        }
    }

    protected void loadMaBaoCao() {
        ComboBoxTagModel model = new ComboBoxTagModel();
        
        listBaoCao = baoCaoBUS.getAll();
        for (int i=0; i<listBaoCao.size(); i++) {
            BaoCaoCongNo data = (BaoCaoCongNo) listBaoCao.get(i);
            model.addElement(data.getMaBaoCaoNo(), data.getMaBaoCaoNo());
        }
        
        cmbMaBaoCao.setModel(model);
        loadThongTinBaoCao(0);
    }
    
    protected void loadThongTinBaoCao(int index) {
        
        if (listBaoCao != null && index >= listBaoCao.size()) return;
        
        BaoCaoCongNo data = (BaoCaoCongNo) listBaoCao.get(index);
        txtThang.setText(String.valueOf(data.getThang()));
        txtNam.setText(String.valueOf(data.getNam()));
        
        // load table
        loadTable(data.getMaBaoCaoNo());
    }
    
    protected void clearTable() {
        DefaultTableModel model = (DefaultTableModel) tbChiTiet.getModel();
        for (int i=0; i<tbChiTiet.getRowCount(); i++) {
            model.removeRow(i);
        }
    }
    
    protected void loadTable(int maBaoCao) {
        clearTable();
        listCTBaoCao = baoCaoBUS.getDetail(maBaoCao);
        
        if (listCTBaoCao == null) return;
        
        DefaultTableModel model = (DefaultTableModel) tbChiTiet.getModel();
        for (int i=0; i<listCTBaoCao.size(); i++) {
            CTBaoCaoCongNo data = (CTBaoCaoCongNo) listCTBaoCao.get(i);
            Object[] row = {
                data.getMaCTBCCN(),
                data.getMaKH(),
                data.getNoCuoi()
            };
            model.addRow(row);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel19 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        txtSDT = new javax.swing.JTextField();
        txtHoTen = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        txtDiaChi = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtThang = new javax.swing.JTextField();
        txtGioiTinh = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbChiTiet = new javax.swing.JTable();
        jToolBar2 = new javax.swing.JToolBar();
        btnLapBaoCao = new javax.swing.JButton();
        txtNam = new javax.swing.JTextField();
        cmbMaBaoCao = new javax.swing.JComboBox();

        jLabel19.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel19.setText("Mã báo cáo :");

        jLabel6.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel6.setText("Địa chỉ");

        jLabel18.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel18.setText("Tháng :");

        txtEmail.setEditable(false);
        txtEmail.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        txtSDT.setEditable(false);
        txtSDT.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        txtHoTen.setEditable(false);
        txtHoTen.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        jLabel9.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel9.setText("Email");

        jLabel10.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel10.setText("Điện thoại");

        jLabel7.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel7.setText("Giới tính");

        jLabel17.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel17.setText("Năm");

        txtDiaChi.setEditable(false);
        txtDiaChi.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        jLabel8.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel8.setText("Họ tên");

        txtThang.setEditable(false);
        txtThang.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        txtGioiTinh.setEditable(false);
        txtGioiTinh.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        tbChiTiet.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã chi tiết", "Mã khách hàng", "Tiền Nợ"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbChiTiet.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(tbChiTiet);

        jToolBar2.setRollover(true);

        btnLapBaoCao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/Add.png"))); // NOI18N
        btnLapBaoCao.setText("Lập báo cáo");
        btnLapBaoCao.setFocusable(false);
        btnLapBaoCao.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnLapBaoCao.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnLapBaoCao.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnLapBaoCaoMouseClicked(evt);
            }
        });
        jToolBar2.add(btnLapBaoCao);

        txtNam.setEditable(false);
        txtNam.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        cmbMaBaoCao.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cmbMaBaoCao.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbMaBaoCaoItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 825, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel18)
                            .addComponent(jLabel17)
                            .addComponent(jLabel19))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtThang, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                            .addComponent(txtNam, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                            .addComponent(cmbMaBaoCao, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6)
                            .addComponent(jLabel8))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtHoTen)
                                    .addComponent(txtDiaChi))
                                .addGap(52, 52, 52)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel10))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtSDT)
                                    .addComponent(txtEmail)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtGioiTinh, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtSDT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel9))
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel19)
                            .addComponent(jLabel8)
                            .addComponent(txtHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbMaBaoCao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel18)
                            .addComponent(txtThang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel17)
                            .addComponent(txtNam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(txtGioiTinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(34, 34, 34)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                .addGap(20, 20, 20))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnLapBaoCaoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLapBaoCaoMouseClicked
        createReport();
        loadMaBaoCao();
    }//GEN-LAST:event_btnLapBaoCaoMouseClicked

    private void cmbMaBaoCaoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbMaBaoCaoItemStateChanged
        int index = cmbMaBaoCao.getSelectedIndex();
        if ( -1 == index ) return;        
       
        loadThongTinBaoCao(index);
    }//GEN-LAST:event_cmbMaBaoCaoItemStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLapBaoCao;
    private javax.swing.JComboBox cmbMaBaoCao;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JTable tbChiTiet;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtGioiTinh;
    private javax.swing.JTextField txtHoTen;
    private javax.swing.JTextField txtNam;
    private javax.swing.JTextField txtSDT;
    private javax.swing.JTextField txtThang;
    // End of variables declaration//GEN-END:variables

}
